<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>   
package ${basepackage}.${namespace}.ctrl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.cyou.fz.commons.mybatis.selecterplus.mybatis.bean.Paged;
import com.cyou.fz.commons.mybatis.selecterplus.mybatis.bean.Query;
import com.cyou.fz.commons.mybatis.selecterplus.mybatis.bean.Query.DBOrder;
import com.fj.hiwetop.exception.BusinessException;
import com.fj.hiwetop.sys.pojo.Attach;
import com.fj.hiwetop.sys.service.AttachService;
import com.fj.hiwetop.util.ajax.Response;
import com.fj.hiwetop.util.ajax.ResponseFactory;
import com.fj.hiwetop.common.CommonConstant;
import com.fj.hiwetop.util.ContextUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.fj.hiwetop.util.SysConfig;
import javax.servlet.http.HttpServletRequest;
import com.cyou.fz.commons.mybatis.selecterplus.mybatis.util.ColumnUtils;
import com.fj.hiwetop.util.ErrorPageUtil;

import ${basepackage}.${namespace}.pojo.${className};
import ${basepackage}.${namespace}.service.${className}Service;

/**
 * ${table.remarks}
 */
@Controller
@RequestMapping(value = "oper/${classNameLower}")
public class ${className}Controller {
	private static final Logger logger = LoggerFactory.getLogger(${className}Controller.class);
	private static final String OPER_PATH = "hiwetop_oper/${namespace}/${className}/";
	
	@Autowired
	private ${className}Service ${classNameLower}Service;
	
	@Autowired
	private AttachService attachService;
	
	/**
	 * 查询列表
	 * @param pageNo 当前页面
	 * @param pageSize　每页条数
	 * @param ${classNameLower}　实体
	 * @return
	 */
	@RequestMapping("/goList")
	@ResponseBody
	public Response<Paged<${className}>> goList(Integer pageNo, Integer pageSize,${className} ${classNameLower}) {
		Response<Paged<${className}>> r = new Response<Paged<${className}>>();
		Query<${className}> query = Query.build(${className}.class);
		query.setPaged(pageNo , pageSize);
		
		/**********设置查询参数***************/
//		query.addLike("title", title);
//		query.addEq("status", NoticePO.YES);
		query.addOrder("createTime", DBOrder.DESC);
		
		Paged<${className}> res = ${classNameLower}Service.findPagedByQuery(query);
		r.setData(res);
		return r;
	}
	
	/**
	 * 添加、更新操作
	 * @param ${classNameLower}　实体
	 * @return
	 */
	@RequestMapping("/doSaveOrUpdate")
	@ResponseBody
	public Response<String> doSaveOrUpdate(${className} ${classNameLower}) {
		try {
			${classNameLower}Service.saveOrUpdate${className}(${classNameLower});
			return ResponseFactory.getDefaultSuccessResponse();
		} catch (BusinessException e) {
			logger.error(e.getMessage(),e);
			return ResponseFactory.getDefaultFailureResponse(CommonConstant.FAILD_MSG);
		}
	}
	
	/**
	 * 查看详细
	 * @param id 主键id
	 * @return
	 */
	@RequestMapping("/goView")
	public ModelAndView goView(String id) {
		ModelAndView mv = new ModelAndView(OPER_PATH+"view");
		if(StringUtils.isNotBlank(id)){
			${className} res = ${classNameLower}Service.get(id);
			mv.addObject("${classNameLower}", res);
			return mv;
		}
		return mv;
	}
	
	/**
	 * 进入新增或编辑页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/goUpdate")
	public ModelAndView goUpdate(String id) {
		ModelAndView mv = new ModelAndView(OPER_PATH+"update");
		if(StringUtils.isNotBlank(id)){
			${className} res = ${classNameLower}Service.get(id);
			mv.addObject("${classNameLower}", res);
			return mv;
		}
		return mv;
	}
	
	/**
	 * 删除操作
	 * @param id 主键id
	 * @return
	 */
	@RequestMapping("/doDel")
	@ResponseBody
	public Response<String> doDel(String ids) {
		try {
			if(StringUtils.isNotBlank(ids)){
				for (String id : ids.split(",")) {
					${classNameLower}Service.delete(id);
				}
			}
			return ResponseFactory.getDefaultSuccessResponse();
		} catch (BusinessException e) {
			logger.error(e.getMessage(),e);
			return ResponseFactory.getDefaultFailureResponse(CommonConstant.FAILD_MSG);
		}
	}
	
	/**
	 * 进入上传列表展示图片页面
	 * 
	 * @param id
	 *            主键id
	 * @return
	 */
	@RequestMapping("/goUpload")
	public ModelAndView goUpload(String id) {
		String tableName = ColumnUtils.getTableName(${className}.class);
		String bizCode = SysConfig.ATTACH_BIZ_CODE_COMMON; //自定义code

		List<Attach> attachs = attachService.getAttachList(tableName, id, bizCode);

		ModelAndView modelAndView = new ModelAndView(CommonConstant.UPLOAD_JSP);
		modelAndView.addObject("attachs", attachs);
		modelAndView.addObject("bizId", id);
		modelAndView.addObject("url", ContextUtil.getRequestBasePath() + "oper/${classNameLower}/doUpload.do");

		return modelAndView;
	}
	
	/**
	 * 上传图片操作
	 * @param myfiles
	 * @param request
	 * @return
	 */
	@RequestMapping("/doUpload")
	public ModelAndView doUpload(@RequestParam MultipartFile[] myfiles,HttpServletRequest request){
		try {
			String bizId = request.getParameter("bizId");
			String tableName = ColumnUtils.getTableName(${className}.class);
			String bizCode = SysConfig.ATTACH_BIZ_CODE_COMMON; //自定义code
			int type = Attach.TYPE_IMAGE;
			String des = request.getParameter("des");
			for (MultipartFile file : myfiles) {
				attachService.saveUniqueFile(file, tableName, des, type, bizId,bizCode);
			}
			return goUpload(bizId);
		}  catch (Exception e) {
			return ErrorPageUtil.getUploadErrorPage(e, logger);
		}
	}
}
